<?php
namespace app\api\controller;

use think\Controller;
header("Content-type: text/html; charset=utf-8");

class Dizhi extends Controller{
    //根据经纬度获取地址
    public function jingwei()
    {
        $center = input('jingwei');

//        $center="36.68013,117.06533";
        if (!$center) {
            return '';
        }
        //http://api.map.baidu.com/staticimage/v2?ak=
        //            'key' => config('map.key'),//腾讯地图
        $data = [
            'location' => $center,
            'output' => config('map.output'),
            'ak' => config('map.ak'),
        ];
        $url=config('map.baidumap_url').config('map.geocoder').'?'.http_build_query($data);
        $res = $this->doCurl($url);
        return $res;
    }

    public function doCurl($url,$type=0,$data=[]){
        $ch=curl_init(); //初始化
        //设置选项
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_HEADER,0);
        if ($type==1){
            //post
            curl_setopt($ch,CURLOPT_PORT,1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
        }
        //执行获取内容
        $output=curl_exec($ch);
        //释放curl句柄
        curl_close($ch);
        return $output;
    }
}